package com.hanthink.common.config;

import io.minio.BucketExistsArgs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.minio.MinioClient;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InvalidResponseException;

/**
 * <p>Title: HdfsConfig</p>
 * <p> Description: 分布式对象存储配置</p>
 * @author vondser
 * @date 2020年12月8日
 */
@Configuration
@EnableConfigurationProperties(HdfsProperties.class)
public class HdfsConfig {

	protected final Logger logger = LoggerFactory.getLogger(HdfsConfig.class);

	@Autowired
	private HdfsProperties hdfsProperties;

	/**
	 * <p>Title: minioClient</p>
	 * <p>Description: 获取MinioClient</p>
	 * @return MinioClient
	 */

	@Bean
	public MinioClient minioClient() {
		MinioClient client = null;
		try {
			client = MinioClient.builder()
					.endpoint(hdfsProperties.getEndpoint())
					.credentials(hdfsProperties.getAccesskey(),hdfsProperties.getSecretkey())
					.build();

			// Check whether 'my-bucketname' exists or not.
			boolean found =
					client.bucketExists(BucketExistsArgs.builder().bucket(hdfsProperties.getBucketName()).build());
			if (found) {
				System.out.println("HDFS-Bucket已存在");
			} else {
				System.out.println("HDFS已成功创建Bucket!");
			}
		} catch (ErrorResponseException e) {
			logger.error(e.getMessage());
		} catch (InvalidResponseException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error("minioClient 异常", e);
		}
		return client;
	}

}
